package trans.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import trans.model.Sa;
import trans.model.SaAttachment;
import trans.service.AttachmentDAO;
import trans.service.SADAO;
import trans.support.BasicSupport;
import trans.support.Constants;

import com.opensymphony.xwork2.Preparable;

public class AttachmentMgrAction extends BasicSupport implements Preparable{

	private static final long serialVersionUID = 1L;
	

	Map<Integer,String> usableStates = new LinkedHashMap<Integer,String>();
	Map<Integer,String> categorys = new LinkedHashMap<Integer,String>();
	@Override
	public void prepare() throws Exception {
		initManuShow();
		
		categorys = AttachmentDAO.getCategorys();
	}
	public Map<Integer, String> getUsableStates() {
		return usableStates;
	}
	public void setUsableStates(Map<Integer, String> usableStates) {
		this.usableStates = usableStates;
	}
	
	public Map<Integer, String> getCategorys() {
		return categorys;
	}
	public void setCategorys(Map<Integer, String> categorys) {
		this.categorys = categorys;
	}


	List<SaAttachment> saAttachments = new ArrayList<SaAttachment>();
	public String index() throws Exception{
		initSAStates(attachment.getSaId());
		
		saAttachments = AttachmentDAO.getAllAttachments(attachment.getSaId(),null,currentUser().getUserRole());
		return "attachMgr";
	}
	
	private void initSAStates(Integer saId) {
		Sa sa = SADAO.getSa(saId);
		
		if(Constants.SATYPE_AIR.equals(sa.getSaType())){
			usableStates = Constants.AIRSA_STATES;
		}
		if(Constants.SATYPE_OCEAN.equals(sa.getSaType())){
			usableStates = Constants.OCEANSA_STATES;
		}
	}
	
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String description;

	SaAttachment attachment;
	public String upload() throws Exception{
		System.out.println("upload file begin***********************************");
		AttachmentDAO.addNewAttachment(attachment, upload,uploadFileName);
		addActionMessage(getText("message.attachment.upload.success"));
		return index();
	}
	
	Integer attachmentId;
	Integer saId;
	public String delete()throws Exception{
		initSAStates(saId);
		AttachmentDAO.deleteAttachment(attachmentId);
		addActionMessage(getText("message.attachment.delete.success"));
		
		attachment = new SaAttachment();
		attachment.setSaId(saId);
		
		saAttachments = AttachmentDAO.getAllAttachments(saId,null,currentUser().getUserRole());
		return "attachMgr";
	}
	
	public String show() throws Exception{
		return "show";
	}
	
	public InputStream getInputStream() throws Exception {
		return new FileInputStream(AttachmentDAO.getAttachmentFile(attachmentId));
	}
	
	public String getFileName() throws Exception {
		return AttachmentDAO.getAttachmentFileName(attachmentId);
	}
	
	public String getFileType() throws Exception {
		String fileName = AttachmentDAO.getAttachmentFileName(attachmentId);
		if(fileName.endsWith("gif")){return "image/gif";}
		if(fileName.endsWith("jpg")){return "image/jpg";}
		if(fileName.endsWith("jpeg")){return "image/jpeg";}
		if(fileName.endsWith("png")){return "image/png";}
		
//		if(fileName.endsWith("pdf")){return "application/pdf";}
//		
//		if(fileName.endsWith("xls") || fileName.endsWith("xlsx")){return "application/vnd.ms-excel";}
//		if(fileName.endsWith("docx") || fileName.endsWith("doc")){return "application/vnd.ms-word";}
		
		return "application/octet-stream";
	}
	
	public Integer getAttachmentId() {
		return attachmentId;
	}
	public void setAttachmentId(Integer attachmentId) {
		this.attachmentId = attachmentId;
	}
	public List<SaAttachment> getSaAttachments() {
		return saAttachments;
	}
	public void setSaAttachments(List<SaAttachment> saAttachments) {
		this.saAttachments = saAttachments;
	}
	public SaAttachment getAttachment() {
		return attachment;
	}
	public void setAttachment(SaAttachment attachment) {
		this.attachment = attachment;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Integer getSaId() {
		return saId;
	}
	public void setSaId(Integer saId) {
		this.saId = saId;
	}
}
